package com.D5Power.Objects
{
	import com.D5Power.basic.GraphicsResource;
	
	import flash.geom.Rectangle;

	public class MovieObject extends GameObject
	{
		/**
		 * 当前帧数
		 */ 
		protected var _currentFrame:uint=0;
		
		/**
		 * 上一帧数
		 */ 
		protected var _lastFrame:uint=0;
		
		/**
		 * 动画最大帧数
		 */ 
		protected var _FrameTotal:uint = 0;
		
		/**
		 * 是否循环
		 */ 
		protected var _loop:Boolean = true;
		
		/**
		 * 非循环动作是否播放完毕
		 */ 
		protected var loopPlayEnd:Boolean=false;
		
		/**
		 * 播放时间间隔
		 */ 
		private var _playTime:uint;
		
		/**
		 * 上一帧的播放时间，用于计算两帧间的时间差
		 */ 
		private var _lastFrameTime:uint;
		
		public function MovieObject()
		{
			_lastFrameTime = Global.Timer;
			super();
		}
		
		protected function enterFrame():void
		{
			if(_graphics==null || _graphics.fps==0) return;
			if(Global.Timer-_lastFrameTime>=_playTime && !loopPlayEnd)
			{
				_lastFrameTime = Global.Timer;
				_lastFrame = _currentFrame;
				if(_currentFrame>=graphics.getFrameTotal(Math.abs(directionNum))-1)
				{
					_loop ? _currentFrame=0 : loopPlayEnd=true;
				}else{
					_currentFrame++;
				}
			}
		}
		
		public function set currentFrame(value:int):void
		{
			_currentFrame=value;
		}
		
		public function get currentFrame():int
		{
			return _currentFrame;
		}
		
		
		public function get lastFrame():int
		{
			return _lastFrame;
		}
		
		public function set Loop(b:Boolean):void
		{
			_loop = b;
			loopPlayEnd = false;
		}
		
		override public function set graphics(value:GraphicsResource):void
		{
			super.graphics = value;
			if(_graphics==null) return;
			_playTime = 1000/_graphics.fps;
		}
		
		/**
		 * 渲染矩形
		 */
		override public function get renderRect():Rectangle
		{
			if(_graphics.framesTotal==1)
			{
				return super.renderRect;
			}
			return new Rectangle(currentFrame*graphics.frameWidth,Math.abs(directionNum)*graphics.frameHeight,graphics.frameWidth,graphics.frameHeight);
		}
	}
}